﻿using System;
using System.ComponentModel;

namespace Notoric.Gui
{
    /// <summary>
    /// Holds information about a recently open file.
    /// </summary>
    public class MostRecentFile : INotifyPropertyChanged
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="MostRecentFile"/> instance.
        /// </summary>
        /// <param name="csv">
        /// <para>
        /// Type: <see cref="String"/>
        /// </para>
        /// <para>
        /// A string in a CSV format holding the serialized <see cref="MostRecentFile"/> representation.<br/>
        /// <i>Note: This representation can be retrieved by calling <see cref="ToString"/> method.</i>
        /// </para>
        /// </param>
        public MostRecentFile(string csv)
        {
            var vals = csv.Split(',');

            if (vals.Length != 2)
                throw new ArgumentException("The value is not in a correct format!\n" + csv, "csv");

            Path = vals[0];
            IsPinned = bool.Parse(vals[1]);
        }

        /// <summary>
        /// Creates a new <see cref="MostRecentFile"/> instance.
        /// </summary>
        /// <param name="path">
        /// <para>
        /// Type: <see cref="String"/>
        /// </para>
        /// <para>
        /// The path to the file.
        /// </para>
        /// </param>
        /// <param name="pinned">
        /// <para>
        /// Type: <see cref="Boolean"/>
        /// </para>
        /// <para>
        /// Indicates whether the file is pinned to the recent files list or not.
        /// </para>
        /// </param>
        public MostRecentFile(string path, bool pinned)
        {
            Path = path;
            IsPinned = pinned;
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <inheritdoc/>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Object Members

        /// <inheritdoc/>
        public override string ToString()
        {
            return String.Format("{0},{1}", Path, IsPinned);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the value indicating whether this file is pinned
        /// to the recent files list or not.
        /// </summary>
        public bool IsPinned
        {
            get { return isPinned; }
            set
            {
                isPinned = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("IsPinned"));
            }
        }

        /// <summary>
        /// Gets the name of this file.
        /// </summary>
        public string Name
        {
            get { return name; }
            private set
            {
                name = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }

        /// <summary>
        /// Gets or sets the path to this file.
        /// </summary>
        public string Path
        {
            get { return path; }
            set
            {
                path = value;
                Name = System.IO.Path.GetFileName(path);

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Path"));
            }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the value of the <see cref="IsPinned"/> property.
        /// </summary>
        private bool isPinned;
        /// <summary>
        /// Holds the value of the <see cref="Name"/> property.
        /// </summary>
        private string name;
        /// <summary>
        /// Holds the value of the <see cref="Path"/> property.
        /// </summary>
        private string path;

        #endregion
    }
}
